const App = {
  mixins: [Query],//配置文件，路径/utils/mixins.js
  data() {
    return {
      selectedCount: 0,
      treeData1: [],
      loading: false,
      category: [],
      village: [],
      checkboxOptions: [],
      dialogbforwardVisible: false,
      page: {
        value1: '',
        //value2:'',
        PageNumber: 1, //页码
        RowAmount: 10,
        Condition: window.btoa(unescape(encodeURIComponent("%%,%%"))),
        ExtensionCondition: window.btoa(unescape(encodeURIComponent(" and complaints_processing_records.processor_id in (" + getCookie('unit') + ")"))),
      },
      personExecReceiver: '',
      SelectedRow: [],
      complaintTitle: ''

    }
  },
  mounted() {
    // this.Init();
    this.Setparty_organizationTree();
  },
  methods: {
    MemberConfirm() {
      if (!this.personExecReceiver) {
        this.$message.error("请选择协办人。");
        return;
      }
      this.execReceive(this.SelectedRow);
      this.dialogbforwardVisible = false;

    },
    handleNodeClickUnit(data, node) {
      // this.nodeKey = data.Party_Organization_SerialNumber;
      // this.nodeName = data.Party_Organization_Name;
      //f_government_member_information

      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
        type: "post",
        data: {
          SQLSerialNumber: "f_government_member_information",
          Condition: "",
          ExtensionCondition: window.btoa(
            unescape(
              encodeURIComponent(
                "AND government_member_information.Government_Agencies_SerialNumber='" +
                data.Government_Agencies_SerialNumber + "'"
              )
            )
          ),
        },
        success: (newMembers) => {


          this.checkboxOptions = newMembers;
          console.log("获取到新的人员列表:", this.checkboxOptions);

        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });

    },
    Setparty_organizationTree() {
      const _this = this;



      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
        type: "GET",
        data: {
          TableName: 'government_agencies',
          NodeCode: "",
        },
        success: (data) => {
          _this.treeData1 = data.map((item) => ({
            id: item.Government_Agencies_SerialNumber,
            label: item.Agensies_Name,

            ...item,
          }));
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });

    },

    complaintsAdd() {
      window.location.href = "./add.html?action=Add";
    },

    check() {
      //this.page.Condition = window.btoa(unescape(encodeURIComponent(this.page.value1 + ",%"+this.page.value2+"%")));
      this.page.Condition = window.btoa(unescape(encodeURIComponent("%" + this.page.value1 + "%" + ",%%"))),
        this.postgetList(this.page);
    },

    getCategorys(data, arr) {
      data.forEach((item) => {
        arr.push(item.category_id);
        if (item.children && item.children.length > 0) {
          this.getCategorys(item.children, arr);
        }
      });
    },
    //树的点击筛选事件
    handleNodeClick(data, node) {
      let arr = [];
      if (data.children) {
        this.getCategorys(data.children, arr);
      }
      let strwhereCategory_ids = "";
      [data.category_id, ...arr].forEach((item) => {
        if (strwhereCategory_ids) {
          strwhereCategory_ids += "\r\nOR complaints.category_id='" + item + "'";
        } else {
          strwhereCategory_ids = "\r\complaints.category_id='" + item + "'";
        }
      });
      if (strwhereCategory_ids) {
        strwhereCategory_ids = "AND (" + strwhereCategory_ids + ")";
      };

      this.page.Condition = window.btoa(unescape(encodeURIComponent("%" + this.page.value1 + "%,%%")));

      this.page.ExtensionCondition = window.btoa(unescape(encodeURIComponent(strwhereCategory_ids + " and complaints_processing_records.processor_id in (" + getCookie('unit') + ")")));
      this.postgetList(this.page);
    },

    allocationReceive(row) {


      let tablekey = row.record_id;
      this.complaintTitle = row.title;

      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
        method: "post",
        data: {
          Data: 'complaints_processing_records',
          Condition: "Row_Status='0' and record_id ='" + tablekey + "'",
        },
        success: (res) => {
          if (res[0].Receiver != '') {
            this.$confirm('此事件已有接受人,确定接受?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              // 点击确定

              //判断是否需要协同
              if (row.bforward == '1') {
                //需要协同
                this.checkboxOptions = [];
                this.personExecReceiver = '';
                this.SelectedRow = [];
                this.SelectedRow = res;
                this.dialogbforwardVisible = true;
              }
              else {
                this.personExecReceiver = '';
                this.execReceive(res);
              }
            }).catch(() => {
              // 点击取消
              this.$message({
                type: 'info',
                message: '已取消操作'
              });
            });
          }
          else {
            //判断是否需要协同
            if (row.bforward == '1') {
              //需要协同
              this.checkboxOptions = [];
              this.personExecReceiver = '';
              this.SelectedRow = [];
              this.SelectedRow = res;
              this.dialogbforwardVisible = true;
            }
            else {
              this.personExecReceiver = '';
              this.execReceive(res);
            }
          }

        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });

    },
    execReceive(data) {
      this.loading = true;
      //更新接受人以及接受时间操作
      //通过登录人获取身份证号
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryByTableName`,
        type: "POST",
        data: {
          Data: 'v_userinfo',
          Condition: "Row_Status='0' and User_Number='" + getCookie('user') + "'",
        },
        success: (user) => {
          data[0].Receiver = user[0].ID_SerialNumber;

          if (this.personExecReceiver) {
            data[0].ExecReceiver = this.personExecReceiver;
          }
          else {
            data[0].ExecReceiver = '';
          }



          data[0].ReceiverDatetime = getYMDHMSDate();
          let lJSONObject_DataSet = { Modify: {} };

          lJSONObject_DataSet.Modify.complaints_processing_records = data;

          submitSave(lJSONObject_DataSet, (res) => {
            if (res.Status == 'success') {
              this.$message.success("接受任务成功!");
              //发消息给ExecReceiver
              if (this.personExecReceiver) {



                // let strurl = mh + "/requestForward/requestForward?RequestURL=" +
                //   encodeURIComponent("/wx/mp/sendComplaintsMessage?Data=" + data[0].complaint_id)

                // // console.log("Url", strurl);

                // $.ajax({
                //   headers: requestHeaders(),
                //   url: strurl,
                //   method: "get",
                //   dataType: "json",
                //   success: (data1) => {
                //     // console.log('发送消息给ExecReceiver',data1);
                //   },
                //   complete: (XMLHttpRequest) => {
                //     requestPretreatment(XMLHttpRequest);
                //   },
                // })
                this.seedMessage();
              }

              this.postgetList(this.page);
            }
            this.loading = false;
          })

        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    //改为用这个末班发消息
    // 模板ID4PbnStNuKulqdX2DHrXyTmRfim9aI60bZhj61juhvpY
    // 模板编号327
    // 标题应急发布通知
    // 类目政务服务大厅
    // 操作人__****17 2025-09-14 添加
    // 详细内容发布内容{{thing1.DATA}}
    // 发布单位{{thing4.DATA}}
    // 发布时间{{date3.DATA}}
    // 场景说明会议签到通知
    seedMessage() {
      //获取人员名称以及OPENID
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
        method: "post",
        data: {
          SQLSerialNumber: "SQL_GetUserNameAndOpenID",
          Condition: '',
          ExtensionCondition: window.btoa(unescape(encodeURIComponent(" where personal_information.ID_Card_Number ='" + this.personExecReceiver + "'")))
        },
        success: (res1) => {
          if (res1.length > 0) {
            const lopenid = res1[0].OpenID;
            const lname = res1[0].Personal_Name;
            const lServerdatetime = res1[0].Serverdatetime;
            //调用消息发送接口
            $.ajax({
              headers: requestHeaders('JSON'),
              url: `${mh}/requestForward/requestForwardForPost?RequestURL=${encodeURIComponent(`/wx/mp/sendMicroProgramTemplateMessage`)}`,
              method: "POST",
              data: JSON.stringify({
                openId: lopenid,
                templateId: "4PbnStNuKulqdX2DHrXyTmRfim9aI60bZhj61juhvpY",
                content: {
                  thing1: { value: "您有一项协办事宜，请及时处理！" }, //事件ID：" + this.complaintTitle 
                  thing4: { value: "事件处理模块自动发送。" },
                  date3: { value: lServerdatetime }
                }
              }),
              success: (res) => {

              },
              complete: (XMLHttpRequest) => {
                requestPretreatment(XMLHttpRequest)

              },

            });
          } else {

          }
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
      });



    },
    allocationAdd(row) {


      let { primary_key } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
      window.location.href = "./add.html?action=Modify&nodeKey=" + row[primary_key];

    },

    allocationQuery(row) {
      let { primary_key } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
      window.location.href = "./addQuery.html?action=Modify&nodeKey=" + row[primary_key];

    },
  },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
